
<?php
session_start(); 
if( !$_SESSION["login"])header('Location: error_page.php');
	include("db_util.php");
	putenv("ORACLE_HOME=/usr/local/libexec/oracle/app/oracle/product/11.2.0/client_1");
	$connection=DbConnect();
  $query = "select * from album where AlbID=" . $_GET["AID"];
			$statement1 = oci_parse($connection, $query);
			oci_execute($statement1);
			ocifetch ( $statement1 );
			
	$check=false;

  if($_FILES["file2"]["size"]!=0) $check=UploadPic($_FILES["file2"],$_GET["AID"],$connection, $statement1);
  if($_FILES["file3"]["size"]!=0) $check=UploadPic($_FILES["file3"],$_GET["AID"],$connection, $statement1);
  if($_FILES["file4"]["size"]!=0) $check=UploadPic($_FILES["file4"],$_GET["AID"], $connection, $statement1);
  if($_FILES["file5"]["size"]!=0)$check=UploadPic($_FILES["file5"],$_GET["AID"],$connection, $statement1);
   if($_FILES["file6"]["size"]!=0) $check=UploadPic($_FILES["file6"],$_GET["AID"], $connection,$statement1);
   oci_free_statement($statement1);
	 
	  oci_close($connection);
  if($check==true){ echo "<p>Pictures are successfully uploaded!</p>";
    echo "<p>Return to view the picture you upload, please click <a href=AblumView.php?AID=" . $_GET["AID"] . ">here</a></p>";}
	
 function UploadPic($file, $albumID, $connection, $statement1)
  {
  
	
if ((($file["type"] == "image/gif")
|| ($file["type"] == "image/jpeg")
|| ($file["type"] == "image/pjpeg"))
)
  {
  if ($file["error"] > 0)
    {
    echo "Return Code: " . $file["error"] . "<br />";
    }
  else
    {
   

    if (file_exists("./picture/student/" .$_SESSION["StuID"] . "/" . OCIResult( $statement1, 'NAME' ) . "/" . $file["name"]))
      {
      echo $file["name"] . " already exists. ";
	  echo "<p>Return to upload your pictures, please click <a href=PictureUpload.php?AID=" . $_GET["AID"] . ">here</a></p>";
	  return false;
      }
    else
      {
	 
	
	 $query = "select max(PID) as MAX from photo";
			$stmt = oci_parse($connection, $query);
			oci_execute($stmt);
			$max_id = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
			$next_id = $max_id["MAX"] + 1;
			
	  date_default_timezone_set('EST');
	  $query="Insert into photo values(" . $next_id . ",'"  . $file["name"] . "','" .  "./picture/student/" .$_SESSION["StuID"] . "/" . OCIResult( $statement1, 'NAME' ) . "/'," . $albumID . ",'" . date('d-M-y') . "','Y')"; 
	$statement = oci_parse($connection, $query);
	oci_execute($statement);
	
	
	  //mkdir   ($testdir,   0777); 
	  if (!file_exists("./picture/student/" .$_SESSION["StuID"] . "/" .  OCIResult( $statement1, 'NAME' ) . "/" )) mkdir ("./picture/student/" .$_SESSION["StuID"] . "/" . OCIResult( $statement1, 'NAME' ) . "/", 0777);
      move_uploaded_file($file["tmp_name"],
      "./picture/student/" .$_SESSION["StuID"] . "/" . OCIResult( $statement1, 'NAME' ) . "/" . $file["name"]);
	 // echo "./picture/student/" .$_SESSION["StuID"] . "/" . OCIResult( $statement1, 'NAME' ) . "/";
    return true;
      }
    }
  }
 


}
  
?>
